package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.LoginUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.iview.IdentityAuthenticationView;
import com.rongshu.view.userInfo.presenter.IdentityAuthenticationPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class IdentityAuthenticationActivity extends BaseActivity<IdentityAuthenticationPresenter> implements IdentityAuthenticationView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.txt_authentication_status)
    TextView txt_authentication_status;
    @BindView(R.id.txt_bi_name)
    TextView txt_bi_name;
    @BindView(R.id.txt_mobile)
    TextView txt_mobile;
    //intent
    private Intent intent = null;
    private String bi_name = null;
    private String mobile = null;
    //map
    private Map<String, Object> map = null;
    //boolean
    private boolean isRealName = false;
    //request
    private final int REQUEST_CODE = 0x01;

    @Override
    protected int initLayout() {
        return R.layout.activity_identity_authentication_view;
    }

    @Override
    protected void initView() {
        //presenter
        setPresenter(new IdentityAuthenticationPresenter(this), this);
        //title
        txt_top_title.setText("身份认证");
        //intent
        intent = getIntent();
        if (intent != null) {
            bi_name = intent.getStringExtra(Constant.INTENT_MESSAGE);
            mobile = intent.getStringExtra(Constant.INTENT_DATA);
        }
        txt_bi_name.setText(bi_name);
        txt_mobile.setText(TextUtils.isEmpty(mobile) ? "未绑定" : mobile);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getSelectRealNameAuthentication();
    }

    /**
     * 查询实名认证信息
     */
    public void getSelectRealNameAuthentication() {
        //user_id
        int user_id = getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this);
            return;
        }
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getSelectRealNameAuthentication(
                (int) map.get("user_id"),
                Common.encryptionSign(map));
        map.clear();
    }

    @OnClick({R.id.layout_back, R.id.layout_real_authentication})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                showKeyboard(false);
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.layout_real_authentication://实名认证
                if (isRealName) {
                    toast("已实名认证");
                    return;
                }
                intent = new Intent(this, RealAuthenticationActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE://实名认证
                    getSelectRealNameAuthentication();
                    break;
            }
        }
    }

    /**
     * 实名认证信息
     *
     * @param bean
     */
    @Override
    public void onRealNameAuthentication(INetBean bean) {
        if (bean.getErrCode() == 0) {
            if (bean.getErrMsg().equals("未实名认证")) {
                isRealName = false;
                txt_authentication_status.setText("未认证");
            } else {
                isRealName = true;
                txt_authentication_status.setText("已认证");
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        showKeyboard(false);
    }
}
